//
//  YWWorkNoticeMessageCell.m
//  maintain
//
//  Created by Kyo on 16/4/20.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWWorkNoticeMessageCell.h"
#import "UIView+Extension.h"
#import "UIColor+CustomColors.h"
#import "YWMacro.h"
#import "Masonry.h"
#import "YWCommonTool.h"
#import "YWTempMission.h"
#import "YWTempMissionManager.h"
#import "YWNotice.h"
#import "YWNoticeManager.h"
#import "YWHandler.h"
#import "UIImageView+WebCache.h"

#import "YWReply.h"
#import "YWReplyManager.h"
#import "YWMaterialApply.h"
#import "YWMaterialStockOut.h"
#import "YWMaterialReturn.h"
#import "YWMaterialManager.h"
#import "YWAssetCheckManager.h"
#import "YWMissionManager.h"
#import "YWInspectionMissionModel.h"
#import "YWInspectionMissionManager.h"
#import "YWKeepWatchMission.h"
#import "YWKeepWatchMissionManager.h"
#import "YWDetectAndUpkeepManager.h"
#import "YWReportTroubleManager.h"
#import "YWReportTroubleModel.h"
#import "YWTroubleMissionModel.h"

#define kavatarImageSize (44)
@interface YWWorkNoticeMessageCell ()
@property (nonatomic, strong) UIImageView* avatarImageView;
@property (nonatomic, strong) UILabel *fromeLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UILabel *relationTypeLabel;
@property (nonatomic, strong) UILabel *relationContentLabel;
@end

@implementation YWWorkNoticeMessageCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *identifier = @"YWWorkNoticeMessageCellIdentifier";
    YWWorkNoticeMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[YWWorkNoticeMessageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        [self autolayoutViews];
    }
    return self;
}

- (void)autolayoutViews{
    CGFloat tempMissionLabelW = 90;
    CGFloat replyLabelW = SCREEN_WIDTH - kMarginW*5 - kavatarImageSize-tempMissionLabelW;
    
    [self.avatarImageView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(self.contentView.mas_top).offset(kTopPadding);
        make.left.mas_equalTo(self.contentView.mas_left).offset(kMarginW);
        make.width.mas_equalTo(kavatarImageSize);
        make.height.mas_equalTo(kavatarImageSize);
    }];
    [self.fromeLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(self.contentView.mas_top).offset(kTopPadding);
        make.left.mas_equalTo(self.avatarImageView.mas_right).offset(kMarginW);
        make.width.mas_equalTo(replyLabelW);
        make.height.mas_equalTo(kMinLabelH);
    }];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(self.fromeLabel.mas_bottom);
        make.left.mas_equalTo(self.fromeLabel.mas_left);
        make.width.mas_equalTo(replyLabelW);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(self.contentLabel.mas_bottom);
        make.left.mas_equalTo(self.contentLabel.mas_left);
        make.width.mas_equalTo(replyLabelW);
        //make.height.mas_equalTo(kMinLabelH);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-kTopPadding);
    }];
    [self.relationTypeLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(self.fromeLabel.mas_top);
        make.left.mas_equalTo(self.contentView.mas_left).offset(SCREEN_WIDTH - tempMissionLabelW-kMarginW);
        make.width.mas_equalTo(tempMissionLabelW);
        make.bottom.mas_equalTo(self.fromeLabel.mas_bottom);
    }];
    [self.relationContentLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(self.contentLabel.mas_top);
        make.left.mas_equalTo(self.contentView.mas_left).offset(SCREEN_WIDTH - tempMissionLabelW-kMarginW);
        make.width.mas_equalTo(tempMissionLabelW);
        make.bottom.mas_equalTo(self.contentLabel.mas_bottom);
    }];
}
-(void)setReply:(YWReply *)reply{
    NSString* downloadUrl = [YWCommonTool pictureDownloadUrl:reply.replier.headIcon];
    [_avatarImageView sd_setImageWithURL:[NSURL URLWithString:downloadUrl] placeholderImage:[UIImage imageNamed:@"profile_headimage"]];
    _fromeLabel.text = reply.replier.userName?:@"";
    if (reply.atUser.userName.length) {
        _contentLabel.text = [NSString stringWithFormat:@"@%@ %@",reply.atUser.userName,reply.content?:@""];
    }else{
        _contentLabel.text = reply.content?:@"";
    }
    NSString *timeStr = [YWCommonTool recordDateByFormatTimeString:reply.replyTime/1000 showYear:YES];
    
    _timeLabel.text = timeStr;
    YWNotice *notice = nil;
    YWTempMission *tempMission = nil;
    _relationTypeLabel.text = @"";
    if (reply.relationType == YWReplyRelationTypeNotice) {
        notice = [[YWNoticeManager shareManager] noticeWithNoticeId:reply.relationId];
        _relationTypeLabel.text = NSLocalizedString(@"notice", nil);//@"通知";
        _relationContentLabel.text = notice.content?:@"";
    }else if (reply.relationType == YWReplyRelationTypeTempMission){
        tempMission = [[YWTempMissionManager shareManager] tempMissionWithTempMissionId:reply.relationId];
        _relationTypeLabel.text = NSLocalizedString(@"tempMission", nil);//@"临时任务";
        _relationContentLabel.text = tempMission.content?:@"";
    }else if (reply.relationType == YWReplyRelationTypeMaterialApply){
        YWMaterialApplyViewModel *viewModel = [[YWMaterialManager shareManager] materialApplyWithMaterialApplyId:reply.relationId];
        _relationTypeLabel.text = NSLocalizedString(@"materialApply", nil);//@"物料申请";
        _relationContentLabel.text = viewModel.applySequence?:@"";
    }else if (reply.relationType == YWReplyRelationTypeMaterialReceive){
        YWMaterialStockOut *receipent = [[YWMaterialManager shareManager] materialReceipientWithMaterialReceipientId:reply.relationId];
        _relationTypeLabel.text = NSLocalizedString(@"materialStockout", nil);//@"物料领用";
        _relationContentLabel.text = receipent.stockOutSequence?:@"";
    }else if (reply.relationType == YWReplyRelationTypeMaterialReturn){
        YWMaterialReturn *materialReturn = [[YWMaterialManager shareManager] materialReturnWithMaterialReturnId:reply.relationId];
        _relationTypeLabel.text = NSLocalizedString(@"materialReturn", nil);//@"物料归还";
        _relationContentLabel.text = materialReturn.returnSequence?:@"";
    }else if (reply.relationType == YWReplyRelationTypeAssetCheckRecord){
        _relationTypeLabel.text = NSLocalizedString(@"checkRecord", nil);//@"清点";
        _relationContentLabel.text = @"";
    }else if (reply.relationType == YWReplyRelationTypeTrouble){
        YWReportTroubleModel *reportTrouble = [[YWReportTroubleManager shareManager] getOneReportTroubleWithTroubleId:reply.relationId];
//        YWTroubleMission *troubleMission = [[YWMissionManager shareManager] troubleMissionWithTroubleId:reply.relationId];
        if (reportTrouble) {
            if (reportTrouble.troubleStatus == YWTroubleMissionStatusUnAssign) {
                _relationTypeLabel.text = NSLocalizedString(@"postTrouble", nil);//@"报障";
            }else{
                _relationTypeLabel.text = NSLocalizedString(@"troubleMission", nil);//@"维修任务";
            }
        }
        _relationContentLabel.text = reportTrouble.troubleSequence?:@"";
    }else if (reply.relationType == YWReplyRelationTypeDetectAndUpkeep){
        YWMaintenanceMission *mission = [YWDetectAndUpkeepManager maintenanceMissionWithMissionId:reply.relationId];
        if (mission) {
            if (mission.maintenanceTypeId == YWMaintenanceTypeDetection) {
                _relationTypeLabel.text = NSLocalizedString(@"detectionMission", nil);//@"检测任务";
            }else{
                _relationTypeLabel.text = NSLocalizedString(@"upkeepMission", nil);//@"保养任务";
            }
        }
        _relationContentLabel.text = mission.missionSequence?:@"";
    }else if (reply.relationType == YWReplyRelationTypeInspection){
        YWInspectionMissionModel *inspectionMission = [[YWInspectionMissionManager shareManager] inspectionMissionWithMissionId:reply.relationId];
        _relationTypeLabel.text = NSLocalizedString(@"inspectionMission", nil);//@"巡查任务";
        _relationContentLabel.text = inspectionMission.missionSequence?:@"";
    }else if (reply.relationType == YWReplyRelationTypeKeepWatch){
        YWKeepWatchMission *keepWatchMission = [[YWKeepWatchMissionManager shareManager] keepWatchMissionWithMissionId:reply.relationId];
        _relationTypeLabel.text = NSLocalizedString(@"keepWatchMission", nil);//@"巡更任务";
        _relationContentLabel.text = keepWatchMission.missionSequence?:@"";
    }
}

- (UIImageView *)avatarImageView{
    if (!_avatarImageView) {
        UIImageView* avatarImageView = [[UIImageView alloc]init];
        avatarImageView.clipsToBounds = YES;
        avatarImageView.layer.cornerRadius = kavatarImageSize / 2;
        avatarImageView.opaque = YES;
        [self.contentView addSubview:avatarImageView];
        _avatarImageView = avatarImageView;
    }
    return _avatarImageView;
}

- (UILabel *)fromeLabel{
    if (!_fromeLabel) {
        UILabel *fromeLabel = [[UILabel alloc]init];
        fromeLabel.font = kBoldContentFont;
        fromeLabel.textColor = [UIColor customBlueColor];
        [self.contentView addSubview:fromeLabel];
        _fromeLabel = fromeLabel;
    }
    return _fromeLabel;
}

-(UILabel *)contentLabel{
    if (!_contentLabel) {
        UILabel *contentLabel = [[UILabel alloc]init];
        contentLabel.font = kContentFont;
        contentLabel.numberOfLines = 0;
        [self.contentView addSubview:contentLabel];
        _contentLabel = contentLabel;
    }
    return _contentLabel;
}
- (UILabel *)timeLabel{
    if (!_timeLabel) {
        UILabel *timeLabel = [[UILabel alloc]init];
        timeLabel.font = kContentFont;
        timeLabel.textColor = [UIColor customTextGrayColor];
        [self.contentView addSubview:timeLabel];
        _timeLabel = timeLabel;
    }
    return _timeLabel;
}
- (UILabel *)relationTypeLabel{
    if (!_relationTypeLabel) {
        UILabel *label = [[UILabel alloc]init];
        label.font = kContentFont;
        label.textColor = [UIColor customYellowColor];
        [self.contentView addSubview:label];
        _relationTypeLabel = label;
    }
    return _relationTypeLabel;
}
-(UILabel *)relationContentLabel{
    if (!_relationContentLabel) {
        UILabel *label = [[UILabel alloc]init];
        label.font = kContentFont;
        label.numberOfLines = 0;
        label.textColor = [UIColor customTextGrayColor];
        [self.contentView addSubview:label];
        _relationContentLabel = label;
    }
    return _relationContentLabel;
}

+ (CGFloat)cellHeightOfReply:(YWReply*)reply{
    CGFloat tempMissionLabelW = 90;
    CGFloat replyLabelW = SCREEN_WIDTH - kMarginW*5 - kavatarImageSize-tempMissionLabelW;
    NSString * contentStr = nil;
    if (reply.atUser.userName.length) {
        contentStr = [NSString stringWithFormat:@"@%@ %@",reply.atUser.userName,reply.content?:@""];
    }else{
        contentStr = reply.content?:@" ";
    }
    CGFloat contenH = [YWCommonTool getHeightOfLabel:contentStr andWidth:replyLabelW andFont:kContentFont andMinHeight:kMinLabelH];
    return contenH + kMinLabelH*2 + kTopPadding*2;
}
@end
